package cn.tedu.mall.book.service.impl;

import cn.tedu.mall.book.entity.Cart;
import cn.tedu.mall.book.ex.ServiceException;
import cn.tedu.mall.book.mapper.CartMapper;
import cn.tedu.mall.book.pojo.dto.CartAddNewDTO;
import cn.tedu.mall.book.pojo.vo.*;
import cn.tedu.mall.book.service.ICartService;
import cn.tedu.mall.book.web.State;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zxjun
 * @date 2022/6/25 10:45
 */
@Service
@Slf4j
public class CartServiceImpl implements ICartService {
    @Autowired
    private CartMapper cartMapper;

    /**
     * 加入购物车分两种情况:
     * 1.我的购物车中没有该商品,则直接加入即可;
     * 2.我的购物车中已有该商品,则要在原有数量上加新加的数量
     *
     * @param cartAddNewDTO
     */
    @Override
    public void addNew(CartAddNewDTO cartAddNewDTO) {
        Long userId = getUserId();
        CartSimpleVO cartSimpleVO = cartMapper.getByUserIdAndProductId(userId, cartAddNewDTO.getProductId());
        if (cartSimpleVO == null) {
            Cart cart = new Cart();
            BeanUtils.copyProperties(cartAddNewDTO, cart);
            cart.setUserId(userId);
            cart.setGmtCreate(LocalDateTime.now());
            cart.setGmtModified(LocalDateTime.now());
            cartMapper.insert(cart);
        }else {
            cartMapper.updateCountById(cartSimpleVO.getId(),cartAddNewDTO.getCount()+cartSimpleVO.getCount());
        }

    }

    @Override
    public void changeCountById(Integer id, Integer count) {
        CartSimpleVO cartSimpleVO = cartMapper.getById(id);
        if (cartSimpleVO == null) {
            throw new ServiceException(State.ERR_NOT_FOUND, "数据异常,所要删除的购物车不存在");
        }
        cartMapper.updateCountById(id, count);
    }

    @Override
    public void deleteById(Integer id) {
        CartSimpleVO cartSimpleVO = cartMapper.getById(id);
        if (cartSimpleVO == null) {
            throw new ServiceException(State.ERR_NOT_FOUND, "数据异常,所要删除的购物车不存在");
        }
        cartMapper.deleteById(id);
    }

    @Override
    public List<CartProductVO> getByUserId() {
        Long id = getUserId();
        List<CartProductVO> cartProductVOs = cartMapper.getByUserId(id);
        constraintNameLength(cartProductVOs, 10);
        return cartProductVOs;
    }

    @Override
    public List<ProductClearVO> getByCartId(Integer[] cartIds) {
        return cartMapper.getByCartId(cartIds);
    }

    /**
     * 约束书名长度
     *
     * @param list
     * @param length
     */
    private void constraintNameLength(List<CartProductVO> list, int length) {
        for (CartProductVO cartProductVO : list) {
            String name = cartProductVO.getName();
            if (name.length() > length) {
                name = name.substring(0, length) + "...";
                cartProductVO.setName(name);
            }
        }
    }
    private Long getUserId(){
        HttpServletRequest  request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
       UserSimpleVO user  = (UserSimpleVO) session.getAttribute("user");
        System.out.println(user);
       Long userId = user.getId();
       return userId;
    }
}
